declare const Promise
declare const require

const fs = require('fs')

export const toDiscipline = function (d) {
  return d.slice(0, 2).toLowerCase()
}

export const slice = Array.prototype.slice

export const resolveBoolean = function (s): Boolean {
  s = s.slice(0, 1).toLowerCase()
  if (s === 'y') return true
  return false
}

export const writeFile = function (name, data) {
  return new Promise((resolve, reject) => {
    fs.writeFile(name, data, 'utf-8', (err) => {
      if (err) return reject(err)
    })
  })
}

export const getRounds = function (participantsLen) {
  let arr = []
  let f = (len) => {
    if (len / 2 > 1) {
      arr.push({ matchesLen: len / 2 })
      f(len / 2)
    }
  }
  f(participantsLen)
  return arr
}

export const range = function (len) {
  let arr = []
  for (let i = 0; i < len; i++) {
    arr.push(i)
  }
  return arr
}

export const getSecondEl = ($el) => {
  return $el.find('a:nth-child(2), a:nth-child(5)')
}

export const getPlayerId = (s) => {
  return s.match(/player=([0-9]+)/)[1]
}

export const getSeed = (s: string): number => {
  let match = s.match(/\[([0-9]+)\]/)
  if (match) {
    return parseInt(match[1])
  }
  return 0
}

export const getQualification = (s: string): Boolean => {
  return s.indexOf('[Q]') > 0
}

export const getWinnerIndex = (a: Array<Array<number>>): Number => {
  a.map(
    el => el.reduce((x, y) => x > y)
  )
}
